<!doctype html>
<html>
  <head>
    <title>Notifications: Requesting notification permission in a detached context should not crash.</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="resources/test-helpers.js"></script>
  </head>
  <body>
    <script>
      // Tests that notifications created in a different context do not crash when said
      // context gets detached, and events are still being fired on it.
      if (window.testRunner) {
          testRunner.setPermission('notifications', 'granted', location.origin, location.origin);
          testRunner.setPopupBlockingEnabled(false);
      }

      async_test(test => {
          const remoteWindow = window.open('resources/window-detached-context.html');
          let remoteNotificationObj = null;

          window.addEventListener('message', test.step_func(event => {
              switch (event.data) {
                  case 'opened':
                      assert_equals(remoteNotificationObj, null);
                      remoteNotificationObj = remoteWindow.Notification;

                      remoteWindow.close();
                      break;

                  case 'closed':
                      assert_not_equals(remoteNotificationObj, null);

                      // Permission is always set to denied for detached objects.
                      assert_equals(remoteNotificationObj.permission, 'denied');

                      // The following call should not throw an exception.
                      remoteNotificationObj.requestPermission(function () {});

                      test.done();
                      break;

                  default:
                      assert_unreached('Unrecognized message from the opened window.');
                      break;
              }
          }));
      }, 'Requesting notification permission in a detached context should not crash.');
    </script>
  </body>
</html>
